<template>
  <van-button
    v-if="is_followed"
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="loading"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFollow"
    :loading="loading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, delFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {},
  model: {
    prop: 'is_followed',
    event: 'update_followed',
  },
  props: {
    is_followed: {
      type: Boolean,
      required: true,
    },
    aut_id: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onFollow() {
      this.loading = true
      try {
        if (this.is_followed) {
          await delFollow(this.aut_id)
        } else {
          await addFollow(this.aut_id)
        }
        this.$emit('update_followed', !this.is_followed)
      } catch (err) {
        let message = '操作失败'
        if (err.response && err.response.status === 400) {
          message = '不能关注自己'
        }
        this.$toast.fail(message)
      }
      this.loading = false
    },
  },
}
</script>

<style lang="less" scoped></style>
